﻿//The author of this class is Sike Mullivan(http://www.codeproject.com/Members/Skully1022)
//Licence The Code Project Open License (CPOL) 1.02 (http://www.codeproject.com/info/cpol10.aspx)

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Collections;
using System.Drawing;

namespace Org.Eurekaa.DNJ.UI.Common
{
    internal static class UIServiceHelper
    {
        // Methods
        public static Font GetDialogFont(IServiceProvider serviceProvider)
        {
            if (serviceProvider != null)
            {
                IUIService service = (IUIService)serviceProvider.GetService(typeof(IUIService));
                if (service != null)
                {
                    IDictionary styles = service.Styles;
                    if (styles != null)
                    {
                        return (Font)styles["DialogFont"];
                    }
                }
            }
            return null;
        }

        public static IWin32Window GetDialogOwnerWindow(IServiceProvider serviceProvider)
        {
            if (serviceProvider != null)
            {
                IUIService service = (IUIService)serviceProvider.GetService(typeof(IUIService));
                if (service != null)
                {
                    return service.GetDialogOwnerWindow();
                }
            }
            return null;
        }

        public static ToolStripRenderer GetToolStripRenderer(IServiceProvider serviceProvider)
        {
            if (serviceProvider != null)
            {
                IUIService service = (IUIService)serviceProvider.GetService(typeof(IUIService));
                if (service != null)
                {
                    IDictionary styles = service.Styles;
                    if (styles != null)
                    {
                        return (ToolStripRenderer)styles["VsRenderer"];
                    }
                }
            }
            return null;
        }

        public static DialogResult ShowDialog(IServiceProvider serviceProvider, Form form)
        {
            if (serviceProvider != null)
            {
                IUIService service = (IUIService)serviceProvider.GetService(typeof(IUIService));
                if (service != null)
                {
                    return service.ShowDialog(form);
                }
            }
            return form.ShowDialog();
        }

        public static void ShowError(IServiceProvider serviceProvider, string message)
        {
            if (serviceProvider != null)
            {
                IUIService service = (IUIService)serviceProvider.GetService(typeof(IUIService));
                if (service != null)
                {
                    service.ShowError(message);
                    return;
                }
            }
            MessageBox.Show(null, message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1, 0);
        }

        public static void ShowError(IServiceProvider serviceProvider, Exception ex, string message)
        {
            if (ex != null)
            {
                message = message + Environment.NewLine + Environment.NewLine + ex.Message;
            }
            if (serviceProvider != null)
            {
                IUIService service = (IUIService)serviceProvider.GetService(typeof(IUIService));
                if (service != null)
                {
                    service.ShowError(message);
                    return;
                }
            }
            MessageBox.Show(null, message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1, 0);
        }

        public static void ShowMessage(IServiceProvider serviceProvider, string message)
        {
            if (serviceProvider != null)
            {
                IUIService service = (IUIService)serviceProvider.GetService(typeof(IUIService));
                if (service != null)
                {
                    service.ShowMessage(message);
                    return;
                }
            }
            MessageBox.Show(null, message, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1, 0);
        }

        public static DialogResult ShowMessage(IServiceProvider serviceProvider, string message, string caption, MessageBoxButtons buttons)
        {
            if (serviceProvider != null)
            {
                IUIService service = (IUIService)serviceProvider.GetService(typeof(IUIService));
                if (service != null)
                {
                    return service.ShowMessage(message, caption, buttons);
                }
            }
            return MessageBox.Show(null, message, caption, buttons, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1, 0);
        }
    }

}
